<?php

namespace app\api\controller;

use app\api\model\Bonus;
use app\api\model\Recharge;
use app\api\model\Giftpack;
use app\api\model\Monthcard;
use app\api\model\Config;
use app\api\model\MonthcardOrder;
use app\api\model\GiftpackOrder;
use app\api\model\RechargeOrder;
require_once VENDOR_PATH.'Wxpay/lib/WxPay.Api.php';
require_once VENDOR_PATH.'Wxpay/example/WxPay.JsApiPay.php';
class Activity extends Api
{
    protected $noNeedLogin = ['monthcard_list','giftpack_list','recharge_list','giftpack_payUrl','monthcard_payUrl','recharge_payUrl'];
    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
    }

    //月卡活动
    public function monthcard_list(){
        $desc = Config::where(['name'=>'monthcard'])->value('value');
        $list = Monthcard::where(['status'=>'1'])->field('id,image,price,tian,bonus,content')->order('sort desc')->select();
        if(!empty($list)){
            $list = collection($list)->toArray();
        }

        //查询优惠券
        $postsaux = Bonus::column("id,name",'id');

        foreach($list as &$v){
            $bonus = json_decode($v['bonus'],true);
            foreach($bonus as $key=>$val){
                $bonus[$key]['name'] = $postsaux[$val['bonus_id']];
            }
            $v['bonus'] = $bonus;
        }
        $return = [
            'desc'      =>  $desc,
            'list'      =>  $list
        ];
        return_msg(0,'查询成功',$return);
    }

    //购买月卡
    public function monthcard_pay(){
        $id = input('id','');//活动id
        $info = Monthcard::where(['id'=>$id,'status'=>'1'])->find();
        if(empty($info)){
            return_msg(1,'该活动已结束');
        }
        $orders = [
            'order_sn'      =>      get_trade_no(),
            'users_id'      =>      $this->userInfo['id'],
            'money'         =>      $info['price'],
            'add_time'      =>      time(),
            'monthcard_id'  =>      $info['id'],
            'bonus'         =>      $info['bonus'],
            'tian'          =>      $info['tian']
        ];
        $order_id = MonthcardOrder::insertGetId($orders);

        //拉起支付
        $input = new \WxPayUnifiedOrder();
        $input->SetBody('月卡购买');
        $input->SetAttach('月卡购买');
        $input->SetOut_trade_no($orders['order_sn']);
        $input->SetTotal_fee($orders['money']*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $payReturn = 'http://'.$_SERVER['HTTP_HOST'].url('Activity/monthcard_payUrl');
        $input->SetOpenid($this->userInfo['openid']);
        $input->SetNotify_url($payReturn);
        $input->SetTrade_type("JSAPI");
        $order = \WxPayApi::unifiedOrder($input);
        if($order['return_msg']=="OK"){
            $tools = new \JsApiPay();
            $jsApiParameters = $tools->GetJsApiParameters($order);
            $jsApiParameters['mch_id'] = $order['mch_id'];
            $jsApiParameters['prepay_id'] = $order['prepay_id'];
            return_msg(0,'微信支付调用成功',['order_id'=>$order_id,'money'=>$orders['money'],'pay'=>$jsApiParameters]);
        }else{
            return_msg(1,'微信支付调用失败');
        }
    }

    //支付回调
    public function monthcard_payUrl()
    {
        controller('Monthcardnotify')->Handle(false);
    }

    //礼包活动
    public function giftpack_list(){
        $desc = Config::where(['name'=>'giftpack'])->value('value');
        $list = Giftpack::where(['status'=>'1'])->field('id,name,image,price,tian,num,content')->order('sort desc')->select();
        if(!empty($list)){
            $list = collection($list)->toArray();
        }
        $return = [
            'desc'      =>  $desc,
            'list'      =>  $list
        ];
        return_msg(0,'查询成功',$return);
    }

    //购买礼包
    public function giftpack_pay(){
        $id = input('id','');//活动id
        $info = Giftpack::where(['id'=>$id,'status'=>'1'])->find();
        if(empty($info)){
            return_msg(1,'该活动已结束');
        }
        $orders = [
            'order_sn'      =>      get_trade_no(),
            'users_id'      =>      $this->userInfo['id'],
            'money'         =>      $info['price'],
            'add_time'      =>      time(),
            'giftpack_id'   =>      $info['id'],
            'bonus_id'      =>      $info['bonus_id'],
            'tian'          =>      $info['tian'],
            'num'           =>      $info['num']
        ];
        $order_id = GiftpackOrder::insertGetId($orders);

        //拉起支付
        $input = new \WxPayUnifiedOrder();
        $input->SetBody('礼包购买');
        $input->SetAttach('礼包购买');
        $input->SetOut_trade_no($orders['order_sn']);
        $input->SetTotal_fee($orders['money']*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $payReturn = 'http://'.$_SERVER['HTTP_HOST'].url('Activity/giftpack_payUrl');
        $input->SetOpenid($this->userInfo['openid']);
        $input->SetNotify_url($payReturn);
        $input->SetTrade_type("JSAPI");
        $order = \WxPayApi::unifiedOrder($input);
        if($order['return_msg']=="OK"){
            $tools = new \JsApiPay();
            $jsApiParameters = $tools->GetJsApiParameters($order);
            $jsApiParameters['mch_id'] = $order['mch_id'];
            $jsApiParameters['prepay_id'] = $order['prepay_id'];
            return_msg(0,'微信支付调用成功',['order_id'=>$order_id,'money'=>$orders['money'],'pay'=>$jsApiParameters]);
        }else{
            return_msg(1,'微信支付调用失败');
        }
    }

    //支付回调
    public function giftpack_payUrl()
    {
        controller('Giftpacknotify')->Handle(false);
    }

    //充值活动
    public function recharge_list(){
        $desc = Config::where(['name'=>'recharge'])->value('value');
        $list = Recharge::where(['status'=>'1'])->field('id,money,price,tian,bonus,content')->order('sort desc')->select();
        if(!empty($list)){
            $list = collection($list)->toArray();
        }
        //查询优惠券
        $postsaux = Bonus::column("id,name",'id');
        foreach($list as &$v){
            $bonus = json_decode($v['bonus'],true);
            foreach($bonus as $key=>$val){
                $bonus[$key]['name'] = $postsaux[$val['bonus_id']];
            }
            $v['bonus'] = $bonus;
        }
        $return = [
            'desc'      =>  $desc,
            'list'      =>  $list
        ];
        return_msg(0,'查询成功',$return);
    }

    //充值支付
    public function recharge_pay(){
        $id = input('id','');//活动id
        $info = Recharge::where(['id'=>$id,'status'=>'1'])->find();
        if(empty($info)){
            return_msg(1,'该活动已结束');
        }
        $orders = [
            'order_sn'      =>      get_trade_no(),
            'users_id'      =>      $this->userInfo['id'],
            'money'         =>      $info['price'],
            'add_time'      =>      time(),
            'recharge_id'   =>      $info['id'],
            'zmoney'        =>      $info['money'],
            'bonus'         =>      $info['bonus'],
            'tian'          =>      $info['tian']
        ];
        $order_id = RechargeOrder::insertGetId($orders);

        //拉起支付
        $input = new \WxPayUnifiedOrder();
        $input->SetBody('充值');
        $input->SetAttach('充值');
        $input->SetOut_trade_no($orders['order_sn']);
        $input->SetTotal_fee($orders['money']*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $payReturn = 'http://'.$_SERVER['HTTP_HOST'].url('Activity/recharge_payUrl');
        $input->SetOpenid($this->userInfo['openid']);
        $input->SetNotify_url($payReturn);
        $input->SetTrade_type("JSAPI");
        $order = \WxPayApi::unifiedOrder($input);
        if($order['return_msg']=="OK"){
            $tools = new \JsApiPay();
            $jsApiParameters = $tools->GetJsApiParameters($order);
            $jsApiParameters['mch_id'] = $order['mch_id'];
            $jsApiParameters['prepay_id'] = $order['prepay_id'];
            return_msg(0,'微信支付调用成功',['order_id'=>$order_id,'money'=>$orders['money'],'pay'=>$jsApiParameters]);
        }else{
            return_msg(1,'微信支付调用失败');
        }
    }

    //支付回调
    public function recharge_payUrl()
    {
        controller('Rechargenotify')->Handle(false);
    }

}